from pydantic_settings import BaseSettings, SettingsConfigDict
from pydantic import Field
from typing import List
import os

class Settings(BaseSettings):
    #数据库的配置
    db_host: str = Field(..., env="DB_HOST")
    db_port: int = Field(..., env="DB_PORT")
    db_user: str = Field(..., env="DB_USER")
    db_password: str = Field(..., env="DB_PASSWORD")
    db_name: str = Field(..., env="DB_NAME")
    #比如其他配置
    http_timeout: int =Field(default=5,env="HTTP_TIMEOUT")
    base_url: str = Field(...,env="BASE_URL")
    #应用环境
    app_env: str = Field(default="dev",env="APP_ENV")
    # 添加 Kafka 相关配置
    kafka_bootstrap_servers: str = Field(default="localhost:9092", env="KAFKA_BOOTSTRAP_SERVERS")
    kafka_topic: str = Field(default="my-topic", env="KAFKA_TOPIC")
    # class Config:
    #     env_file = os.getenv("ENV_FILE", ".env")
    #     env_file_encoding = "utf-8"

    # In Pydantic v2, Config is replaced with model_config
    model_config = SettingsConfigDict(
        env_file=os.getenv("ENV_FILE", ".env"),
        env_file_encoding="utf-8",
        extra = "allow"  # 允许额外字段
    )

    def db_link(self,ty:str="mysql")->str:
        # mysql+aiomysql://root:password@127.0.0.1:3316/test
        return f"mysql+aiomysql://{self.db_user}:{str(self.db_password)}@{self.db_host}:{str(self.db_port)}/{self.db_name}"
        ## self.db_host + ":" + str(self.db_port) + "/" + ty + "/" + self.db_name

settings  = Settings()